package com.dkd.manage.service.impl;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.*;
import com.dkd.manage.domain.vo.ChannelVo;
import com.dkd.manage.domain.vo.OrderVo;
import com.dkd.manage.domain.vo.VendingMachineVo;
import com.dkd.manage.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author yuwei
 * @date 2024-09-02
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private IVmTypeService vmTypeService;

    @Autowired
    private INodeService nodeService;


    @Autowired
    private IChannelService channelService;

    @Autowired
    private IOrderService orderService;


    @Autowired
    private ITaskService taskService;

    @Autowired
    private ITaskDetailsService taskDetailsService;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */

    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {

//        1.新增设备
//        设备编号生成工具
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
//        查询设备类型获取设备容量
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
//        获取点位数据复制给设备
        Node node =nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtil.copyProperties(node,vendingMachine,"id");
        vendingMachine.setAddr(node.getAddress());
//        设备状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);

        //        2.新增货道

        List<Channel> channelList =new ArrayList<>();

for (int i = 1; i <= vmType.getVmRow(); i++){//外层行
    for (int j = 1; j <= vmType.getVmCol(); j++) {//内层列
//        1-1 1-2 1-3
//        2-1 2-2 2-3
//        封装售货机对象
        Channel channel = new Channel();
        channel.setVmId(vendingMachine.getId());//售货机id
        channel.setChannelCode(i+"-"+j);//售货机货道编号
        channel.setInnerCode(vendingMachine.getInnerCode());//售货机编号
        channel.setMaxCapacity(vmType.getChannelMaxCapacity());//售货机容量
        channel.setCreateTime(DateUtils.getNowDate());//创建时间

//        添加数据
        channelList.add(channel);
    }
}

channelService.batchInsertChannels(channelList);


        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {

        //如果是修改策略，那就不涉及点位数据，因为修改策略前段不传nodeId参数
        if(vendingMachine.getNodeId()!=null){
            //        获取点位数据修改设备的点位数据
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtil.copyProperties(node,vendingMachine,"id");//商圈类型,区域id,合作商id
            vendingMachine.setAddr(node.getAddress());//详细地址
        }

        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.updateVendingMachine(vendingMachine);
        return result;
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 根据设备编号查询设备信息
     * @param innerCode
     * @return
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }

    /**
     * 根据设备编号查询设备详情
     * @param id
     * @return
     */
    @Override
    public VendingMachineVo selectVMDetailsSaleById(Long id) {
        //返回的数据
        VendingMachineVo vendingMachineVo =new VendingMachineVo();
        vendingMachineVo.setVMId(id);
        //查询设备
        VendingMachine vendingMachine = vendingMachineMapper.selectVendingMachineById(id);
        //查询销售额和销售数量
        OrderVo orderVo = orderService.selectOrderVoListByInnerCode(vendingMachine.getInnerCode());
        vendingMachineVo.setSaleNum(orderVo.getSaleNum());
        vendingMachineVo.setSaleMoney(orderVo.getSaleMoney());
        //查询补货次数和维修次数
        Task task = new Task();
        task.setInnerCode(vendingMachine.getInnerCode());
        task.setTaskStatus(DkdContants.TASK_TYPE_SUPPLY);
        vendingMachineVo.setReplenishNum((long) taskService.selectTaskByInnerCodeAndStatus(task));
        task.setTaskStatus(DkdContants.TASK_TYPE_REPAIR);
        vendingMachineVo.setRepairNum((long) taskService.selectTaskByInnerCodeAndStatus(task));

        //设备具体情况
        List<ChannelVo> channelVos = channelService.selectChannelVoSaleListByInnerCode(vendingMachine.getInnerCode());
        //判断货道集合里是否有货道为空，有的话就筛选出来
        List<ChannelVo> emptyChannels = channelVos.stream()
                .filter(channelVo -> channelVo.getSkuId() == null)
                .collect(Collectors.toList());
        vendingMachineVo.setChannelVoList(emptyChannels);


        return vendingMachineVo;
    }
}
